package org.example.src.util;

import org.example.src.dto.response.ApiResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * 响应工具类 - 提供统一的响应构建方法
 */
public class ResponseUtil {

    /**
     * 构建成功响应
     * @param data 响应数据
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<ApiResponse<T>> success(T data) {
        return ResponseEntity.ok(ApiResponse.success(data));
    }

    /**
     * 构建带消息的成功响应
     * @param message 响应消息
     * @param data 响应数据
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<ApiResponse<T>> success(String message, T data) {
        return ResponseEntity.ok(ApiResponse.success(message, data));
    }

    /**
     * 构建创建成功的响应 (HTTP 201)
     * @param data 响应数据
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<ApiResponse<T>> created(T data) {
        return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success(data));
    }

    /**
     * 构建错误响应 (HTTP 400)
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<ApiResponse<T>> error(String message) {
        return ResponseEntity.badRequest().body(ApiResponse.error(message));
    }

    /**
     * 构建未找到资源的响应 (HTTP 404)
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<ApiResponse<T>> notFound(String message) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ApiResponse.error(message));
    }

    /**
     * 构建未授权的响应 (HTTP 401)
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<ApiResponse<T>> unauthorized(String message) {
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ApiResponse.error(message));
    }

    /**
     * 构建禁止访问的响应 (HTTP 403)
     * @param message 错误消息
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<ApiResponse<T>> forbidden(String message) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ApiResponse.error(message));
    }
}
